#include <callback.h>
#include <timeheap.h>
#include <time_value.h>
#include <stdlib.h>
#include <stdio.h>

static TimeHeap s_timeheap;

class TimeOut : public CallbackObject
{
public:
	TimeOut()
		: m_timerid( -1 ),
		  m_count( 100 )
	{
		set_handler( this, &TimeOut::handle_request );
		m_timerid = s_timeheap.register_timerhandle( Time_Value(0,10000), Time_Value(0, 10000),
            static_cast<CallbackObject*>(this), NULL );
		fprintf( stderr, "start time(%lu)\n", time(NULL) );
	}
	
	~TimeOut() throw()
	{
		s_timeheap.unregister_timerhandle( m_timerid );
	}
	
	int handle_request( int code, void * data )
	{		
		fprintf( stderr, "m_count(%d) time(%lu)\n", m_count, time(NULL) );
		
		if( 0 == --m_count )
		{
			return -1;
		}
		return 0;
	}

private:
	int	m_timerid;
	int m_count;
};

int main()
{
	TimeOut * timeout1 = new TimeOut();
	
	while( 1 )
	{
		Time_Value now = Time_Value::timeofday();
		s_timeheap.timer_poll( now );
		usleep(10);
	}
	
	delete timeout1;
	return 0;
}

